package com.jdf.org.controller;

import com.jdf.basic.util.AjaxResult;
import com.jdf.basic.util.PageList;
import com.jdf.org.domain.Department;
import com.jdf.org.domain.Employee;
import com.jdf.org.query.DepartmentQuery;
import com.jdf.org.query.EmployeeQuery;
import com.jdf.org.service.EmployeeService;
import com.jdf.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmpController {
    @Autowired
    private EmployeeService employeeService;
    /* 找一 */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findOne(@PathVariable("id")Long id){
        return employeeService.load(id);

    }
    /* 找全部 */
    @GetMapping
    @ApiOperation(value = "查询全部" )
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }
    /*
    添加与修改
     */
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    /*
    删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@PathVariable ("id") Long id){
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    /*
      高级查询
   */
    @PostMapping
    @ApiOperation(value = "高级查询" )
    public PageList<Employee> gaoji(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryData(employeeQuery);
    }
    // 批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult pathDelte(@RequestBody List<Long> ids){
        try {
            employeeService.pathDelte(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


}
